import 'package:encrypt/encrypt.dart';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter/services.dart';
import 'package:pointycastle/asymmetric/api.dart';

/// RSA工具类
class RsaUtils {
  RsaUtils._();

  static final RsaUtils _singleton = RsaUtils._();

  factory RsaUtils() => _singleton;

  static const _pubKeyPath = "assets/rsa/login.pub.pem";

  ///RSA加密
  Future<String> encryption(final String raw) async {
    final parser = RSAKeyParser();
    final pubKey = await rootBundle.loadString(_pubKeyPath);
    debugPrint('public key => $pubKey');
    final RSAPublicKey publickKey = parser.parse(pubKey) as RSAPublicKey;
    final encrypter = Encrypter(RSA(publicKey: publickKey));
    return encrypter.encrypt(raw).base64;
  }
}

/// Rsa加密单例
final rsa = RsaUtils();
